@echo off
title Python 3.11.3 打包修复工具
echo =================================
echo    Python 3.11.3 打包修复工具
echo =================================
echo.

:: 颜色设置
setlocal enabledelayedexpansion

:: 检测Python 3.11.3安装
echo 正在检测Python 3.11.3安装...
echo.

set PYTHON_PATH=
set PYTHON_CMD=

:: 方法1: 使用py命令
echo 1. 尝试使用Python启动器(py)...
py -3.11 --version >nul 2>&1
if %errorlevel% equ 0 (
    set PYTHON_CMD=py -3.11
    goto :found_python
)

:: 方法2: 检查注册表
echo 2. 检查注册表中的Python 3.11...
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.11\InstallPath" >nul 2>&1
if %errorlevel% equ 0 (
    for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.11\InstallPath" /ve') do set PYTHON_PATH=%%b
    if exist "!PYTHON_PATH!\python.exe" (
        set PYTHON_CMD="!PYTHON_PATH!\python.exe"
        goto :found_python
    )
)

:: 方法3: 检查用户注册表
reg query "HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.11\InstallPath" >nul 2>&1
if %errorlevel% equ 0 (
    for /f "tokens=2*" %%a in ('reg query "HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.11\InstallPath" /ve') do set PYTHON_PATH=%%b
    if exist "!PYTHON_PATH!\python.exe" (
        set PYTHON_CMD="!PYTHON_PATH!\python.exe"
        goto :found_python
    )
)

:: 方法4: 扫描常见安装路径
echo 3. 扫描常见安装路径...
set SEARCH_PATHS[0]=C:\Program Files\Python311
set SEARCH_PATHS[1]=C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python311
set SEARCH_PATHS[2]=C:\Python311
set SEARCH_PATHS[3]=D:\Python311
set SEARCH_PATHS[4]=E:\Python311

for /l %%i in (0,1,4) do (
    if exist "!SEARCH_PATHS[%%i]!\python.exe" (
        set PYTHON_PATH=!SEARCH_PATHS[%%i]!
        set PYTHON_CMD="!SEARCH_PATHS[%%i]!\python.exe"
        goto :found_python
    )
)

:: 方法5: 检查PATH中的Python
echo 4. 检查PATH中的Python...
where python >nul 2>&1
if %errorlevel% equ 0 (
    for /f "tokens=*" %%i in ('where python') do (
        "%%i" --version 2>nul | findstr "3.11" >nul
        if !errorlevel! equ 0 (
            set PYTHON_CMD=python
            goto :found_python
        )
    )
)

:: 未找到Python
echo ❌ 未找到Python 3.11.3安装
echo.
echo 解决方案：
echo 1. 重新安装Python 3.11.3并勾选"Add Python to PATH"
echo 2. 使用完整路径运行：
echo    "C:\Program Files\Python311\python.exe" -m pip install pyinstaller
echo    "C:\Program Files\Python311\python.exe" -m PyInstaller --onefile simple_activation_calculator.py
echo.
pause
exit /b 1

:found_python
echo ✅ 找到Python 3.11.3！
echo 📍 路径: %PYTHON_CMD%
echo.

:: 显示版本
%PYTHON_CMD% --version
echo.

:: 检查pip
%PYTHON_CMD% -m pip --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ pip未安装，正在安装...
    %PYTHON_CMD% -m ensurepip
)

:: 安装PyInstaller
echo 正在安装PyInstaller...
%PYTHON_CMD% -m pip install pyinstaller --upgrade
echo.

:: 开始打包
echo 🔨 开始打包...
echo ⏰ 预计用时: 3-5分钟
echo.

%PYTHON_CMD% -m PyInstaller --onefile --name="活化能计算器" --add-data="ExpDat_1-8.csv;." --add-data="ExpDat_1-7.csv;." --hidden-import="scipy.special._ufuncs" --hidden-import="matplotlib.backends.backend_tkagg" --clean --noconfirm simple_activation_calculator.py

echo.
if exist "dist\活化能计算器.exe" (
    echo ✅ 打包成功！
    echo 📁 文件位置: %cd%\dist\活化能计算器.exe
    echo 📊 文件大小:
    for %%I in ("dist\活化能计算器.exe") do echo    %%~zI 字节
    echo.
    echo 🎯 使用方法:
    echo    双击 dist\活化能计算器.exe 即可运行
    echo    无需管理员权限，无需安装Python
    echo.
    explorer dist
) else (
    echo ❌ 打包失败
    echo 📋 请检查网络连接和文件完整性
)

pause